package com.mlnx.netty.col;


import com.mlnx.common.entity.Response;
import com.mlnx.device.pojo.dto.AutoDetectCmdDto;
import com.mlnx.device.service.XmBpDeviceService;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

/**
 * Created by amanda.shan on 2019/3/29.
 */
@RestController
@RequestMapping(value = "/data/xmBp")
public class XmBpController extends BaseController {

    @Autowired
    private XmBpDeviceService xmBpDeviceService;

//    @Autowired
//    private MutilDeviceSessionManager sessionManager;
//
//    private MyLog log = MyLog.getLog(this.getClass());
//
//    ExecutorService executor = Executors.newFixedThreadPool(20);
//
//    private void runSendTask(AutoDetectTask task) {
//        try {
//
//            AutoDetectCmd autoDetectCmd = task.getAutoDetectCmd();
//            boolean success = xmBpControllService.sendAutoDetect(autoDetectCmd);
//
//            if (success) {
//                task.getResponseDeferredResult().setResult(new Response());
//            } else {
//                task.getResponseDeferredResult().setResult(new Response("0001", "设置失败"));
//                Channel channel = sessionManager.getChannel(autoDetectCmd.getDeviceId());
//                sessionManager.removeSession(channel);
//                log.info("无法向血压计发送指令，移除该血压计:{}", autoDetectCmd.getDeviceId());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    @PostMapping(value = "time")
    public DeferredResult<Response> setDetectTime(@RequestBody AutoDetectCmdDto autoDetectCmd) {

//        DeferredResult<Response> deferredResult = new DeferredResult<>();
//
//        // 1、判断设备是否在线
//        Channel channel = sessionManager.getChannel(autoDetectCmd.getDeviceId());
//        if (channel == null) {
//            deferredResult.setResult(new Response("0002", "设备不在线"));
//            logger.error("设备不在线");
//            return deferredResult;
//        }
//        AutoDetectTask detectTask = new AutoDetectTask();
//        detectTask.setAutoDetectCmd(autoDetectCmd);
//        detectTask.setResponseDeferredResult(deferredResult);
//
//        executor.submit(new Runnable() {
//            @Override
//            public void run() {
//                runSendTask(detectTask);
//            }
//        });

        return xmBpDeviceService.setDetectTime(autoDetectCmd);
    }

    @PostMapping(value = "state")
    public DeferredResult<Response> setState(@RequestBody AutoDetectCmdDto autoDetectCmd) {
//        DeferredResult<Response> deferredResult = new DeferredResult<>();
//        // 1、判断设备是否在线
//        Channel channel = sessionManager.getChannel(autoDetectCmd.getDeviceId());
//        if (channel == null) {
//            deferredResult.setResult(new Response("0002", "设备不在线"));
//            logger.error("设备不在线");
//            return deferredResult;
//        }
//        AutoDetectTask detectTask = new AutoDetectTask();
//        detectTask.setAutoDetectCmd(autoDetectCmd);
//        detectTask.setResponseDeferredResult(deferredResult);
//        executor.submit(new Runnable() {
//            @Override
//            public void run() {
//                runSendTask(detectTask);
//            }
//        });

        return xmBpDeviceService.setState(autoDetectCmd);
    }


    @PostMapping(value = "detect")
    public DeferredResult<Response> setdetect(@RequestBody AutoDetectCmdDto autoDetectCmd) {
//        DeferredResult<Response> deferredResult = new DeferredResult<>();
//        // 1、判断设备是否在线
//        Channel channel = sessionManager.getChannel(autoDetectCmd.getDeviceId());
//        if (channel == null) {
//            deferredResult.setResult(new Response("0002", "设备不在线"));
//            logger.error("设备不在线");
//            return deferredResult;
//        }
//        AutoDetectTask detectTask = new AutoDetectTask();
//        detectTask.setAutoDetectCmd(autoDetectCmd);
//        detectTask.setResponseDeferredResult(deferredResult);
//        executor.submit(new Runnable() {
//            @Override
//            public void run() {
//                runSendTask(detectTask);
//            }
//        });

        return xmBpDeviceService.setDetect(autoDetectCmd);
    }

    /**
     * 矫正时间 清空记录 重启编程
     *
     * @param autoDetectCmd
     * @return
     */
    @PostMapping(value = "rectify")
    public DeferredResult<Response> setRectify(@RequestBody AutoDetectCmdDto autoDetectCmd) {
//        DeferredResult<Response> deferredResult = new DeferredResult<>();
//        // 1、判断设备是否在线
//        Channel channel = sessionManager.getChannel(autoDetectCmd.getDeviceId());
//        if (channel == null) {
//            deferredResult.setResult(new Response("0002", "设备不在线"));
//            logger.error("设备不在线");
//            return deferredResult;
//        }
//        AutoDetectTask detectTask = new AutoDetectTask();
//        detectTask.setAutoDetectCmd(autoDetectCmd);
//        detectTask.setResponseDeferredResult(deferredResult);
//        executor.submit(new Runnable() {
//            @Override
//            public void run() {
//
//                try {
//                    runSendTask(detectTask);
//                    log.info("矫正时间");
//                    Thread.sleep(1000);
//                    channel.writeAndFlush(Unpooled.copiedBuffer(CmdUtils.beginCrc));
//                    Thread.sleep(1000);
//                    channel.writeAndFlush(Unpooled.copiedBuffer(CmdUtils.delRecordCrc));
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        });

        return xmBpDeviceService.setRectify(autoDetectCmd);
    }

    /**
     * 同步数据
     *
     * @param autoDetectCmd
     * @return
     */
    @PostMapping(value = "syndata")
    public DeferredResult<Response> synData(@RequestBody AutoDetectCmdDto autoDetectCmd) {
//        DeferredResult<Response> deferredResult = new DeferredResult<>();
//        // 1、判断设备是否在线
//        Channel channel = sessionManager.getChannel(autoDetectCmd.getDeviceId());
//        if (channel == null) {
//            deferredResult.setResult(new Response("0002", "设备不在线"));
//            logger.error("设备不在线");
//            return deferredResult;
//        }
//
//        SynData synData = new SynData();
//        synData.setResponseDeferredResult(deferredResult);
//        synData.setOverTime(0);
//        synData.setCurrentSign(true);
//        SynDataManage.getInstance().add(autoDetectCmd.getDeviceId(), synData);

        return xmBpDeviceService.synData(autoDetectCmd);
    }


}
